// pages/focus/focus.js
Page({
  data: {
    edit:false,
    whole:false,
    ids:[],
    love: [{ src: '../img/pro.jpg', name: '仅售226元！价值312元的4人自助午餐，提供免费WiFi。', url: '../pro/pro', id: '1', now: 0}, { src: '../img/pro.jpg', name: '仅售226元！价值312元的4人自助午餐，提供免费WiFi。', url: '../pro/pro', id: '12', now: 0}]
  },
  all:function(){
    var that=this;
    var datas=this.data.love;
    var ids=[];
    if(that.data.whole==false){
      for (var i = 0; i < datas.length; i++) {
        ids.push(datas[i].id)
        that.setData({
        ['love[' + i + '].now']: 1,
        ids:ids
        });
      };
      that.setData({
        whole:true
      }); 
    }else{
      for (var i = 0; i < datas.length; i++) {
        that.setData({
          ['love[' + i + '].now']: 0
        });
      };
      that.setData({
        whole: false,
        ids: []
      }); 
    }

  },
  pick:function(e){
    var that = this
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    let pid = that.data.ids.findIndex(item => item=== id);
    var ids=that.data.ids;
    if(pid==-1){
      ids.push(id);
    }else{
      ids.splice(pid,1);
    }
    that.setData({
      ['love[' + index + '].now']: that.data.love[index].now == 0 ? 1 : 0,
      ids:ids
    });  
    if(ids.length==that.data.love.length){
      that.setData({
        whole: true
      }); 
    }  
  },
  edit:function(){
    this.setData({ 
      edit:!this.data.edit
    })
  },
  delete: function (e) {
    var that = this;
    var ids = that.data.ids;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          that.setData({ menu: datas })
          for (var i = 0; i < ids.length; i++) {
            var datas = that.data.love;
            let index = datas.findIndex(item => item.id === ids[i]);
            datas.splice(index, 1);
            if(i==ids.length-1){
              that.setData({
               love: datas,
               ids:[]
              });
            }
          };

        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onLoad: function (options) {
  
  },
  onReady: function () {
  
  }
})